#!/bin/bash

# this is executed in the docker fpm image
set -e
cd /build

# add SNAPSHOT if it was requested
VERSION={{.version}}
if [ "$SNAPSHOT" = "yes" ]; then
    VERSION="${VERSION}-SNAPSHOT"
fi

mkdir /{{.beat_name}}-${VERSION}-windows-{{.win_arch}}
cp -a homedir/. /{{.beat_name}}-${VERSION}-windows-{{.win_arch}}/
cp {{.beat_name}}-windows-{{.arch}}.exe /{{.beat_name}}-${VERSION}-windows-{{.win_arch}}/{{.beat_name}}.exe
unix2dos {{.beat_name}}-win.yml
cp {{.beat_name}}-win.yml /{{.beat_name}}-${VERSION}-windows-{{.win_arch}}/{{.beat_name}}.yml
cp {{.beat_name}}-win.reference.yml /{{.beat_name}}-${VERSION}-windows-{{.win_arch}}/{{.beat_name}}.reference.yml
cp fields.yml /{{.beat_name}}-${VERSION}-windows-{{.win_arch}}/
cp -a modules.d-win/ /{{.beat_name}}-${VERSION}-windows-{{.win_arch}}/modules.d || true
cp install-service-{{.beat_name}}.ps1 /{{.beat_name}}-${VERSION}-windows-{{.win_arch}}/
cp uninstall-service-{{.beat_name}}.ps1 /{{.beat_name}}-${VERSION}-windows-{{.win_arch}}/

mkdir -p upload
zip -r upload/{{.beat_name}}-${VERSION}-windows-{{.win_arch}}.zip /{{.beat_name}}-${VERSION}-windows-{{.win_arch}}
echo "Created upload/{{.beat_name}}-${VERSION}-windows-{{.win_arch}}.zip"

cd upload
sha1sum {{.beat_name}}-${VERSION}-windows-{{.win_arch}}.zip | awk '{print $1;}' > {{.beat_name}}-${VERSION}-windows-{{.win_arch}}.zip.sha1
echo "Created upload/{{.beat_name}}-${VERSION}-windows-{{.win_arch}}.zip.sha1"
